package com.scau.service.Impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.scau.dao.Impl.VideoDaoImpl;
import com.scau.pojo.Video;
import com.scau.service.VideoService;
@Service
public class VideoServiceImpl implements VideoService{
	@Autowired
	VideoDaoImpl videoDAO;

	@Override
	public List<Video> query(int seId) {
		return videoDAO.query(seId);
	}

	@Override
	public int add(JSONObject jsonObject) {
		Video video=new Video();
		String vName=jsonObject.getString("name");
		int seId=jsonObject.getInteger("seId");
		int vSeq=jsonObject.getInteger("vSeq");
		video.setvName(vName);
		video.setSeId(seId);;
		video.setvSeq(vSeq);
		return videoDAO.add(video);
	}

	@Override
	public int delete(JSONArray jsonArray){
		int result=0;
		List<Integer> idArr=JSON.parseArray(jsonArray.toString(), Integer.class);
		try {
			for(int id : idArr){
				if(videoDAO.delete(id)!=1){
					break;
					
				}else{
					result=1;
				}
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return result;
	}

	@Override
	public int update(JSONObject jsonObject) {
		int result=0;
		Video video=new Video();
		int vId=jsonObject.getInteger("id");
		String vName=jsonObject.getString("name");
		int seId=jsonObject.getInteger("seId");
		int vSeq=jsonObject.getInteger("vSeq");
		video.setvName(vName);
		video.setSeId(seId);
		video.setvSeq(vSeq);
		video.setvId(vId);
		try {
			result=videoDAO.update(video);
			
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return result;
	}

	@Override
	public int getVideoCount(int seId) {
		return videoDAO.countBySeId(seId);
	}

	@Override
	public int isNameExist(String vName,Integer seId) {
		return videoDAO.isNameExist(vName,seId);
	}

	@Override
	public int isSeqExist(Integer vSeq,Integer seId) {
		return videoDAO.isSeqExist(vSeq,seId);
	}
}
